package com.hspedu.homework.Homework02;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

/**
 * @author DL5O
 * @version 1.0
 */
public class ReceiveA {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(8888);
        byte[] buff = new byte[1024];
        //用一个包来接受SenderB
        DatagramPacket packet = new DatagramPacket(buff, buff.length);
        System.out.println("等待接受...");
        socket.receive(packet);//等待SenderB发送数据然后封装到packet中去

        byte[] data = packet.getData();//拆包，取出数据
        int length = packet.getLength();
        String s = new String(data,0,length);
        System.out.println(s);

        //发送数据给SenderB
        Scanner sc = new Scanner(System.in);
        byte[] info = ("四大名著是"+sc.next()).getBytes();
        DatagramPacket packet1 = new DatagramPacket(info, info.length, InetAddress.getByName("192.168.170.1"), 8887);
        socket.send(packet1);

        System.out.println("数据发送完毕");
        System.out.println("ReceiveA退出");
        socket.close();
    }
}
